package com.zs.backend.modules.mobileManagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zs.backend.common.api.CommonPage;
import com.zs.backend.common.api.CommonResult;
import com.zs.backend.modules.mobileManagement.dto.MobileManagementDTO;
import com.zs.backend.modules.mobileManagement.model.MobileManagement;
import com.zs.backend.modules.mobileManagement.service.MobileManagementService;
import com.zs.backend.modules.userQueryPwd.model.UserQueryPwd;
import com.zs.backend.modules.userQueryPwd.service.UserQueryPwdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 手机管理表 前端控制器
 * </p>
 *
 * @author zs
 * @since 2024-03-18
 */

@RestController
@Api(tags = "手机管理")
@RequestMapping("/mobileManagement")
public class MobileManagementController {

    @Autowired
    private MobileManagementService managementService;

    @Autowired
    private UserQueryPwdService userQueryPwdService;

    // 管理员导入Excel
    @ApiOperation("管理员导入Excel(form-data 传参)")
    @RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult uploadExcel(
        @RequestParam(value = "file", required = false)  MultipartFile file) {
        boolean success = managementService.uploadExcel(file);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }



    @ApiOperation("手机管理列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MobileManagementDTO>> list(
        @ApiParam(value = "地区",name = "area")
        @RequestParam(value = "area", required = false) String area,

        @ApiParam(value = "地区 code", name = "areaCode")
        @RequestParam(value = "areaCode", required = false) String areaCode,

        @ApiParam(value = "管理人员", name = "managementUser")
        @RequestParam(value = "managementUser", required = false) String managementUser,

        @ApiParam(value = "年限-开始", name = "startYearTime")
        @RequestParam(value = "startYearTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startYearTime,

        @ApiParam(value = "年限-结束", name = "endYearTime")
        @RequestParam(value = "endYearTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endYearTime,

        @ApiParam(value = "手机号码", name = "tel")
        @RequestParam(value = "tel", required = false) String tel,
        @ApiParam(value = "身份证号", name = "idCard")
        @RequestParam(value = "idCard", required = false) String idCard,

        @ApiParam(value = "姓名", name = "name")
        @RequestParam(value = "name", required = false) String name,

        @ApiParam(value = "证书编号", name = "certificateNumber")
        @RequestParam(value = "certificateNumber", required = false) String certificateNumber,

        @ApiParam(value = "专业", name = "major")
        @RequestParam(value = "major", required = false) String major,

        @ApiParam(value = "职称分类", name = "titleCategory")
        @RequestParam(value = "titleCategory", required = false) String titleCategory,

        @ApiParam(value = "拨打电话时间-开始", name = "startCallTime")
        @RequestParam(value = "startCallTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startCallTime,

        @ApiParam(value = "拨打电话时间-结束", name = "endCallTime")
        @RequestParam(value = "endCallTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endCallTime,


        @ApiParam(value = "状态（0：未使用;1：已使用）", name = "status")
        @RequestParam(value = "status", required = false) Integer status,

        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        MobileManagementDTO dto = MobileManagementDTO.builder()
            .area(area)
            .areaCode(areaCode)
            .managementUser(managementUser)
            .startYearTime(startYearTime)
            .endYearTime(endYearTime)
            .tel(tel)
            .idCard(idCard)
            .name(name)
            .certificateNumber(certificateNumber)
            .major(major)
            .titleCategory(titleCategory)
            .startCallTime(startCallTime)
            .endCallTime(endCallTime)
            .status(status)
            .build();
        Page<MobileManagementDTO> pageList = managementService.list(dto, pageSize,pageNum);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("添加、修改手机管理(form-data 传参)")
    @RequestMapping(value = "/addOrUpt", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addOrUpt(
        @ApiParam(value = "数据id，修改时必传",name = "id")
        @RequestParam(value = "id", required = false) Long id,

        @ApiParam(value = "地区",name = "area")
        @RequestParam(value = "area", required = false) String area,

        @ApiParam(value = "地区 code", name = "areaCode")
        @RequestParam(value = "areaCode", required = false) String areaCode,

        @ApiParam(value = "管理人员", name = "managementUser")
        @RequestParam(value = "managementUser", required = false) String managementUser,

        @ApiParam(value = "年限", name = "yearTime")
        @RequestParam(value = "yearTime", required = false)  @DateTimeFormat(pattern = "yyyy-MM-dd") Date yearTime,

        @ApiParam(value = "手机号码", name = "tel")
        @RequestParam(value = "tel", required = false) String tel,
        @ApiParam(value = "身份证号", name = "idCard")
        @RequestParam(value = "idCard", required = false) String idCard,

        @ApiParam(value = "姓名", name = "name")
        @RequestParam(value = "name", required = false) String name,

        @ApiParam(value = "证书编号", name = "certificateNumber")
        @RequestParam(value = "certificateNumber", required = false) String certificateNumber,

        @ApiParam(value = "专业", name = "major")
        @RequestParam(value = "major", required = false) String major,

        @ApiParam(value = "职称分类", name = "titleCategory")
        @RequestParam(value = "titleCategory", required = false) String titleCategory,

        @ApiParam(value = "备注", name = "remark")
        @RequestParam(value = "remark", required = false) String remark,

        @ApiParam(value = "拨打电话时间（yyyy-MM-dd HH:mm:ss）", name = "callTime")
        @RequestParam(value = "callTime", required = false)  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date callTime,

        @ApiParam(value = "状态（0：未使用;1：已使用）", name = "status")
        @RequestParam(value = "status", required = false) Integer status
        ) {
        MobileManagementDTO dto = MobileManagementDTO.builder()
            .area(area)
            .areaCode(areaCode)
            .managementUser(managementUser)
            .yearTime(yearTime)
            .tel(tel)
            .idCard(idCard)
            .name(name)
            .certificateNumber(certificateNumber)
            .major(major)
            .titleCategory(titleCategory)
            .callTime(callTime)
            .status(status)
            .remark(remark)
            .id(id)
            .build();
        boolean success = managementService.addOrUpt(dto);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @ApiOperation("验证查看密码")
    @GetMapping("/checkPwd/{pwd}")
    public CommonResult checkPwd(
        @ApiParam(value = "密码", name = "pwd")
        @PathVariable String pwd){
        boolean result = managementService.checkPwd(pwd);
        return CommonResult.success(result);
    }

    @ApiOperation("设置查看密码")
    @GetMapping("/setPwd/{pwd}")
    public CommonResult setPwd(
        @ApiParam(value = "密码", name = "pwd")
        @PathVariable String pwd){
        QueryWrapper<UserQueryPwd> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UserQueryPwd> lambda = wrapper.lambda();
        lambda.eq(UserQueryPwd::getType, 1);
        UserQueryPwd userQueryPwd = userQueryPwdService.getOne(wrapper);
        if(userQueryPwd != null){
            userQueryPwd.setPwd(pwd);
            userQueryPwdService.updateById(userQueryPwd);
            return CommonResult.success(true);
        }

        boolean result = managementService.setPwd(pwd);
        return CommonResult.success(result);
    }

}

